package com.lijiajia.cloud.authserver.configuration.security;

import com.lijiajia.cloud.authserver.service.MenuService;
import com.lijiajia.cloud.authserver.service.RoleService;
import com.lijiajia.cloud.authserver.service.UserConnectionService;
import com.lijiajia.cloud.authserver.service.UserService;
import com.lijiajia.cloud.authserver.security.form.UsernameUserService;
import com.lijiajia.cloud.authserver.security.oauth.CustomOAuth2UserService;
import com.lijiajia.cloud.authserver.security.oauth.user.GithubUserInfoConverter;
import com.lijiajia.cloud.authserver.security.oauth.user.UserInfoConverter;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.oauth2.client.userinfo.OAuth2UserRequest;
import org.springframework.security.oauth2.client.userinfo.OAuth2UserService;
import org.springframework.security.oauth2.core.user.OAuth2User;

import java.util.HashMap;
import java.util.Map;

/**
 * security bean 配置
 *
 * @author lijiajia
 * @since 2019-05-10
 */
@Configuration
public class SecurityConfiguration {

    /**
     * 用户密码 加密方式
     */
    @Bean
    @Qualifier("userPasswordEncoder")
    public PasswordEncoder userPasswordEncoder() {
        return new BCryptPasswordEncoder();
    }

    @Configuration
    public class UserInfoConverterConfiguration {
        @Bean
        @Qualifier("registerUserInfoConverter")
        Map<String, UserInfoConverter> userInfoConverters() {
            Map<String, UserInfoConverter> userInfoConverters = new HashMap<>();
            userInfoConverters.put("github", new GithubUserInfoConverter());
            return userInfoConverters;
        }
    }

    @Configuration
    public class UserServiceConfiguration {
        private final UserService userService;
        private final RoleService roleService;
        private final MenuService menuService;
        private final UserConnectionService userConnectionService;

        private final Map<String, UserInfoConverter> userInfoConverters;

        public UserServiceConfiguration(UserService userService,
                                        RoleService roleService,
                                        MenuService menuService,
                                        UserConnectionService userConnectionService,
                                        @Qualifier("registerUserInfoConverter") Map<String, UserInfoConverter> userInfoConverters) {
            this.userService = userService;
            this.roleService = roleService;
            this.menuService = menuService;
            this.userConnectionService = userConnectionService;
            this.userInfoConverters = userInfoConverters;
        }

        @Bean("usernameUserService")
        public UserDetailsService userDetailsService() {
            return new UsernameUserService(userService, roleService, menuService);
        }

        @Bean
        @Qualifier("customOAuth2UserService")
        public OAuth2UserService<OAuth2UserRequest, OAuth2User> oAuth2UserService() {
            return new CustomOAuth2UserService(userService, roleService, menuService, userConnectionService, userInfoConverters);
        }
    }
}
